import React from 'react';
import { Table, Image, Tag, Statistic } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import type { ExchangeItem } from '../types';
import { ApiService } from '../services/ApiService';

interface ExchangeListProps {
  data: ExchangeItem[];
  loading: boolean;
}

const ExchangeList: React.FC<ExchangeListProps> = ({ data, loading }) => {
  // 计算收益率
  const getProfitRate = (item: ExchangeItem, resType: number): { value: number; display: string } => {
    if (resType === 0) {
      // 挂刀：从平台购买，卖到Steam
      const buyPrice = Math.min(item.buff_sell_price, item.yyyp_sell_price);
      const sellPrice = item.steam_buy_price * 0.85; // 考虑Steam手续费
      const profitRate = ApiService.calculateProfitRate(buyPrice, sellPrice);
      return { value: profitRate, display: `${profitRate.toFixed(2)}%` };
    } else {
      // 反向挂刀：从Steam购买，卖到平台
      const buyPrice = item.steam_sell_price * 1.15; // 考虑Steam手续费
      const sellPrice = Math.max(item.buff_buy_price, item.yyyp_buy_price);
      const profitRate = ApiService.calculateProfitRate(buyPrice, sellPrice);
      return { value: profitRate, display: `${profitRate.toFixed(2)}%` };
    }
  };

  // 获取最佳平台
  const getBestPlatform = (item: ExchangeItem, resType: number): string => {
    if (resType === 0) {
      // 挂刀：找最低售价
      return item.buff_sell_price < item.yyyp_sell_price ? 'BUFF' : 'YYYP';
    } else {
      // 反向挂刀：找最高求购价
      return item.buff_buy_price > item.yyyp_buy_price ? 'BUFF' : 'YYYP';
    }
  };

  const columns: ColumnsType<ExchangeItem> = [
    {
      title: '饰品图片',
      dataIndex: 'img',
      key: 'img',
      width: 60,
      render: (img: string) => <Image src={img} width={50} height={50} preview={false} />,
    },
    {
      title: '饰品名称',
      dataIndex: 'name',
      key: 'name',
      ellipsis: true,
    },
    {
      title: 'Steam市场名称',
      dataIndex: 'market_hash_name',
      key: 'market_hash_name',
      ellipsis: true,
    },
    {
      title: '最佳平台',
      key: 'best_platform',
      render: (_, record) => {
        const platform = getBestPlatform(record, 0); // 默认挂刀模式
        return (
          <Tag color={platform === 'BUFF' ? 'blue' : 'green'}>
            {platform}
          </Tag>
        );
      },
    },
    {
      title: '平台最低售价',
      key: 'platform_price',
      render: (_, record) => {
        const minPrice = Math.min(record.buff_sell_price, record.yyyp_sell_price);
        return <Statistic value={minPrice} prefix="¥" precision={2} />;
      },
    },
    {
      title: 'Steam求购价',
      key: 'steam_price',
      render: (_, record) => (
        <Statistic value={record.steam_buy_price} prefix="¥" precision={2} />
      ),
    },
    {
      title: '估算收益率',
      key: 'profit_rate',
      render: (_, record) => {
        const { value, display } = getProfitRate(record, 0); // 默认挂刀模式
        return (
          <Tag color={value > 0 ? 'green' : 'red'}>
            {display}
          </Tag>
        );
      },
      sorter: (a, b) => {
        const rateA = getProfitRate(a, 0).value;
        const rateB = getProfitRate(b, 0).value;
        return rateA - rateB;
      },
    },
    {
      title: 'Steam日成交量',
      dataIndex: 'turnover_number',
      key: 'turnover_number',
      sorter: (a, b) => a.turnover_number - b.turnover_number,
    },
    {
      title: '平台在售数量',
      key: 'sell_num',
      render: (_, record) => record.buff_sell_num + record.yyyp_sell_num,
    },
    {
      title: 'Steam求购数量',
      dataIndex: 'steam_buy_num',
      key: 'steam_buy_num',
    },
  ];

  return (
    <Table
      columns={columns}
      dataSource={data}
      rowKey="id"
      loading={loading}
      pagination={{
        pageSize: 20,
        showSizeChanger: true,
        showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，共 ${total} 条`,
      }}
      scroll={{ x: 1200 }}
    />
  );
};

export default ExchangeList;